var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');

var app = getApp();

Page({
  data: {
    orderId:null,
    billing:{
      type: 1,
      title: '',
      number: '',
      address: '',
      tel: '',
      bank: '',
      bankNum: '',
    }
  },
  onLoad: function(options) {
    // 页面初始化 options为页面跳转所带来的参数
    // 页面初始化 options为页面跳转所带来的参数
    this.setData({
      orderId: options.id
    });
  },
  radioChange: function(e) {
    console.log(e.detail.value)
    this.data.billing.type = e.detail.value
    this.setData(this.data);
  },
  //获取checkou信息
  getBillingInfo: function() {
    let that = this;
    util.request(api.BillingInfo, {
     
    }).then(function(res) {
      if (res.errno === 0) {
        that.data.billing=res.data;
        that.setData(that.data);
      }
      wx.hideLoading();
    });
  },
  bindTitleInput: function(e) {
    this.data.billing.title=e.detail.value;
    this.setData(this.data);
  },
  bindNumberInput: function(e) {
    this.data.billing.number=e.detail.value;
    this.setData(this.data);
  },
  bindAddressInput: function(e) {
    this.data.billing.address=e.detail.value;
    this.setData(this.data);
  },
  bindTelInput: function(e) {
    this.data.billing.tel=e.detail.value;
    this.setData(this.data);
  },
  bindBankInput: function(e) {
    this.data.billing.bank=e.detail.value;
    this.setData(this.data);
  },
  bindBankNumInput: function(e) {
    this.data.billing.bankNum=e.detail.value;
    this.setData(this.data);
  },
  onReady: function() {
    // 页面渲染完成

  },
  onShow: function() {
    // 页面显示
    wx.showLoading({
      title: '加载中...',
    });
    this.getBillingInfo();
  },
  onHide: function() {
    // 页面隐藏

  },
  onUnload: function() {
    // 页面关闭

  },
  submitBilling: function() {
    wx.showLoading({
      title:'加载中...',                             
      mask:true                                    
    })
    if(!this.data.orderId){
      util.showErrorLongToast('订单异常');
      return false;
    }
    if (!this.data.billing.title) {
      wx.hideLoading();
      util.showErrorLongToast('请填写开票名称');
      return false;
    }
    if (this.data.billing.type==2 || this.data.billing.type==3) {
      if (!this.data.billing.number) {
        wx.hideLoading();
        util.showErrorLongToast('请填写纳税人识别号');
        return false;
      }
    }
    if ( this.data.billing.type==3) {
      if (!this.data.billing.bank) {
        wx.hideLoading();
        util.showErrorLongToast('请填写开户银行');
        return false;
      }
      if (!this.data.billing.bankNum) {
        wx.hideLoading();
        util.showErrorLongToast('请填写银行账号');
        return false;
      }
      if (!this.data.billing.address) {
        wx.hideLoading();
        util.showErrorLongToast('请填写单位地址');
        return false;
      }
      if (!this.data.billing.tel) {
        wx.hideLoading();
        util.showErrorLongToast('请填写单位电话');
        return false;
      }
    }
    var reqData={};
    reqData.orderId=this.data.orderId;
    reqData.billing=this.data.billing;
    util.request(api.BillingSubmit, reqData , 'POST').then(res => {
      if (res.errno === 0) {
        // 下单成功，重置couponId
        wx.hideLoading();
        wx.showToast({
          title: '发票申请提交成功！',
          icon:'none',
          mask:true,
          success:function(){
            setTimeout(function () {
              //要延时执行的代码
              wx.navigateBack({
                delta: 1,
              })
            }, 1500) //延迟时间
           
          }
        })
       
        
      } else {
        wx.hideLoading();
        util.showErrorToast(res.errmsg);
      }
    });
  }
});